﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SSDTConditions;
using System.Data;

namespace UnitTestProject
{
    [TestClass]
    public class DateTimeValueConditionUnitTest
    {
        [TestMethod]
        public void DateTimeNullValueTestMethod()
        {
            // Arrange
            DateTimeValueCondition condition = new DateTimeValueCondition();
            DateTimeValueCondition.DateTimeValueVerificationCode expected;
            DateTimeValueCondition.DateTimeValueVerificationCode actual;

            DataTable table = new DataTable();
            ArrangeTestConditionTable(table);

            // Null Expected condition is true
            condition.NullExpected = true;
            table.Rows[0][0] = DBNull.Value;
            expected = DateTimeValueCondition.DateTimeValueVerificationCode.Ok;
            // Act 
            actual = condition.DateTimeConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Null Expected condition is false
            condition.NullExpected = true;
            table.Rows[0][0] = "test";
            expected = DateTimeValueCondition.DateTimeValueVerificationCode.NullExptected;
            // Act 
            actual = condition.DateTimeConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void CustomDateTimeFormatterTestMethod()
        {
            string value, actual, expected;
            DateTimeValueCondition.CustomDateTimeFormat format;
            bool success;
            // Arrange
            DateTimeValueCondition condition = new DateTimeValueCondition();

            // system locale format
            DateTime now = System.DateTime.Now;
            // date format will be "dddd, MMMM dd, yyyy" for en-US culture
            // time format will be "h:mm:ss tt" for en-US culture
            value = now.ToLongDateString() + " " + now.ToLongTimeString();
            // expected will be in format "yyyy-MM-dd HH:mm:ss"
            expected = now.ToString("s").Replace("T", " ");
            format = DateTimeValueCondition.CustomDateTimeFormat.System;

            // Act 
            success = condition.CustomDateTimeFormatter(value, format, out actual);

            // Assert
            Assert.AreNotEqual(expected, value);
            Assert.AreEqual(true, success);
            Assert.AreEqual(expected, actual);

            // YMD format
            value = "20090915";
            expected = "2009-09-15";
            format = DateTimeValueCondition.CustomDateTimeFormat.YMD;

            // Act 
            success = condition.CustomDateTimeFormatter(value, format, out actual);

            // Assert
            Assert.AreEqual(true, success);
            Assert.AreEqual(expected, actual);

            // YMD format with T separator
            value = "2009-09-15T1:45:20AM";
            expected = "2009-09-15 01:45:20";
            format = DateTimeValueCondition.CustomDateTimeFormat.YMD;

            // Act 
            success = condition.CustomDateTimeFormatter(value, format, out actual);

            // Assert
            Assert.AreEqual(true, success);
            Assert.AreEqual(expected, actual);

            // DMY format
            value = "15.09.2009 13:45:20";
            expected = "2009-09-15 13:45:20";
            format = DateTimeValueCondition.CustomDateTimeFormat.DMY;
                
            // Act 
            success = condition.CustomDateTimeFormatter(value, format, out actual);

            // Assert
            Assert.AreEqual(true, success);
            Assert.AreEqual(expected, actual);

            // MDY format
            value = "9/5/2009 10:45:20PM";
            expected = "2009-09-05 22:45:20";
            format = DateTimeValueCondition.CustomDateTimeFormat.MDY;

            // Act 
            success = condition.CustomDateTimeFormatter(value, format, out actual);

            // Assert
            Assert.AreEqual(true, success);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void DateTimeConditionTestMethod()
        {
            // Arrange
            DateTimeValueCondition condition = new DateTimeValueCondition();
            DateTimeValueCondition.DateTimeValueVerificationCode expected;
            DateTimeValueCondition.DateTimeValueVerificationCode actual;

            DataTable table = new DataTable();
            ArrangeTestConditionTable(table);

            // Equals condition is true
            condition.ExpectedValue = "6/15/2009T1:45:20PM";
            condition.ExpectedValueFormat = DateTimeValueCondition.CustomDateTimeFormat.MDY;
            table.Rows[0][0] = "2009-06-15 13:45:20";
            expected = DateTimeValueCondition.DateTimeValueVerificationCode.Ok;
            // Act 
            actual = condition.DateTimeConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Equals condition is false
            condition.ExpectedValue = "15.06.2009 13:45:20";
            condition.ExpectedValueFormat = DateTimeValueCondition.CustomDateTimeFormat.DMY;
            table.Rows[0][0] = "2000-01-01 13:45:20";
            expected = DateTimeValueCondition.DateTimeValueVerificationCode.FalseCondition;
            // Act 
            actual = condition.DateTimeConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Wrong Actual value
            condition.ExpectedValue = "2009-06-15 13:45:20";
            condition.ExpectedValueFormat = DateTimeValueCondition.CustomDateTimeFormat.YMD;
            table.Rows[0][0] = "2009-13-15 13:45:20";
            expected = DateTimeValueCondition.DateTimeValueVerificationCode.WrongActualValue;
            // Act 
            actual = condition.DateTimeConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);

            // Wrong Expected value
            condition.ExpectedValue = "20091315 13:45:20";
            condition.ExpectedValueFormat = DateTimeValueCondition.CustomDateTimeFormat.YMD;
            table.Rows[0][0] = "2009-06-15 13:45:20";
            expected = DateTimeValueCondition.DateTimeValueVerificationCode.WrongExpectedValue;
            // Act 
            actual = condition.DateTimeConditionVerification(table);
            // Assert
            Assert.AreEqual(expected, actual);
        }

        private static void ArrangeTestConditionTable(DataTable table)
        {
            // add first column
            table.Columns.Add("1", typeof(string));
            // add first row
            DataRow row = table.NewRow();
            table.Rows.Add(row);
        }
    }
}
